
package com.cy.model;

import com.alibaba.druid.util.StringUtils;
import com.cy.BasicModel;

import java.util.ArrayList;
import java.util.List;
import com.cy.ext.plugin.tablebind.TableBind;
import com.jfinal.plugin.activerecord.Page;

import java.util.Date;

/**
* Created with IntelliJ IDEA.
* User: Administrator
* Date: 13-9-9
* Time: 下午12:56
* To change this template use File | Settings | File Templates.
*/
@SuppressWarnings("serial")
@TableBind(tableName="license",pkName = "license_id")
public class License extends BasicModel<License> {
    public static final License dao = new License();
    public static final String SELECT_ALL = "`license_id`,`user_id`,`license_available`,`license_consumption`,`license_frozen`,`last_os`,`status`";

    public Page<License> paginate(Integer pageNumber, Integer pageSize, String select, License order,String other) {
        List<Object> paras = new ArrayList<>();
        StringBuffer sqlExceptSelectBuffer = new StringBuffer("FROM " + getTableName());
        sqlExceptSelectBuffer.append(other);
        if(StringUtils.isEmpty(other)){
            sqlExceptSelectBuffer.append(" where 1=1 ");
        }
        StringBuffer selectBuffer = new StringBuffer("SELECT ").append(select);
        if (pageNumber == null || pageNumber == 0) {
            pageNumber = 1;
        }
        if (pageSize == null || pageSize == 0) {
            pageSize = 20;
        }

        if (order != null) {
            String[] attrNames = order.getAttrNames();
            for (String attrName : attrNames) {
                Object attrValue = order.get(attrName);
                if (attrValue != null) {

                    if("nick_name".equals(attrName)){
                        sqlExceptSelectBuffer.append(" AND `nick_name` like ? ");
                        paras.add("%"+attrValue+"%");
                    }else{
                        int index = attrName.indexOf(".");
                        if (index > 0){
                            sqlExceptSelectBuffer.append(" AND `"+attrName.substring(0,index)+"`.`" + attrName.substring(index+1) + "`=?");
                        }else {
                            sqlExceptSelectBuffer.append(" AND `" + attrName + "`=?");
                        }
                        paras.add(attrValue);
                    }
                }
            }
        }

        if (paras.size() > 0) {
            return paginate(pageNumber, pageSize, selectBuffer.toString(), sqlExceptSelectBuffer.toString(), paras.toArray());
        }
        return paginate(pageNumber, pageSize, selectBuffer.toString(), sqlExceptSelectBuffer.toString());
    }

    public License fatchLicense( Long num){
        if (num > 0){
            Long afterAvail = this.getLicenseAvailable() + num ;
            this.setLicenseAvailable(afterAvail);
        }
        return  this;
    }

    public License frozen(Long num){
        long afterAvail = this.getLicenseAvailable() - num;
        if (afterAvail >= 0){
            long thisFrozen = this.getLicenseFrozen();
            this.setLicenseAvailable(afterAvail).setLicenseFrozen(thisFrozen + num);
            return this;
        }else{
            throw new NumberFormatException();
        }
    }


    public License unFrozen(Long num){
        long afterFrozen = this.getLicenseFrozen() - num;
        if (afterFrozen >= 0){
            long thisAvai = this.getLicenseAvailable() + num;
            this.setLicenseAvailable(thisAvai).setLicenseFrozen(afterFrozen);
            return this;
        }else{
            throw new NumberFormatException();
        }
    }
      
    public License addToFrozen(Long num){
        long afterFrozen = this.getLicenseFrozen() + num;
        long afterConsu = this.getLicenseConsumption()+num;

        if (afterFrozen >= 0){
            return this.setLicenseFrozen(afterFrozen).setLicenseConsumption(afterConsu);
        }else{
            throw new NumberFormatException();
        }
    }

    public License completeRecharge(Long num){
        long afterFrozen = this.getLicenseFrozen() - num;
        long afterConsu = this.getLicenseConsumption()+num;

        if (afterFrozen >= 0){
            return this.setLicenseFrozen(afterFrozen).setLicenseConsumption(afterConsu);
        }else{
            throw new NumberFormatException();
        }
    }

    public License completeReturn(Long num){
        long afterFrozen = this.getLicenseFrozen() - num;
        long afterConsu = this.getLicenseConsumption() - num;
        long afterAvai = this.getLicenseAvailable() + num;

        if (afterFrozen >= 0){
            return this.setLicenseFrozen(afterFrozen).setLicenseConsumption(afterConsu).setLicenseAvailable(afterAvai);
        }else{
            throw new NumberFormatException();
        }
    }

    public License findByUserId(Long userId){
        return findFirst("select * from "+this.getTableName() +" where user_id = ?" , userId);
    }
    
    public License setLicenseId (Long licenseId){
        this.set("license_id",licenseId);
        return this;
    }

    public Long getLicenseId (){
        return this. get("license_id");
    }

    public License setUserId (Long userId){
        this.set("user_id",userId);
        return this;
    }

    public Long getUserId (){
        return this. get("user_id");
    }

    public License setLicenseAvailable (Long licenseAvailable){
        this.set("license_available",licenseAvailable);
        return this;
    }

    public Long getLicenseAvailable (){
        return this. getLong("license_available");
    }

    public License setLicenseConsumption (Long licenseConsumption){
        this.set("license_consumption",licenseConsumption);
        return this;
    }

    public Long getLicenseConsumption (){
        return this. getLong("license_consumption");
    }

    public License setLicenseFrozen (Long licenseFrozen){
        this.set("license_frozen",licenseFrozen);
        return this;
    }

    public Long getLicenseFrozen (){
        return this. getLong("license_frozen");
    }

    public License setLastOs (String lastOs){
        this.set("last_os",lastOs);
        return this;
    }

    public String getLastOs (){
        return this.getStr("last_os");
    }

        public License setStatus (Integer status){
        this.set("status",status);
        return this;
    }

    public Integer getStatus (){
        return this. get("status");
    }
}
